package javabase.demo01;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Objects;

public class CommonAPIPractice {
    public static void main(String[] args) {
        // String类实践
        String str1 = "hello";
        String str2 = "hello";
        String str3 = new String("hello");
        String str4 = "World";

        // == 和 equals 区别
        System.out.println("str1 == str2: " + (str1 == str2)); // true，字符串常量池同一对象
        System.out.println("str1 == str3: " + (str1 == str3)); // false，不同对象
        System.out.println("str1.equals(str3): " + str1.equals(str3)); // true，内容相同

        // String不可变性演示
        String original = "Hello";
        String modified = original.concat(" World");
        System.out.println("Original: " + original); // 不变
        System.out.println("Modified: " + modified); // 新对象

        // StringBuffer vs StringBuilder
        // StringBuffer是线程安全的，StringBuilder不是但更快
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("Hello").append(" ").append("World");
        System.out.println("StringBuffer: " + stringBuffer.toString());

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("Hello").append(" ").append("World");
        System.out.println("StringBuilder: " + stringBuilder.toString());

        // Object类方法实践
        Person person1 = new Person("Alice", 25);
        Person person2 = new Person("Alice", 25);
        Person person3 = new Person("Bob", 30);

        System.out.println("person1.equals(person2): " + person1.equals(person2));
        System.out.println("person1.equals(person3): " + person1.equals(person3));

        System.out.println("person1 hashCode: " + person1.hashCode());
        System.out.println("person2 hashCode: " + person2.hashCode());
        System.out.println("person3 hashCode: " + person3.hashCode());

        System.out.println("person1 toString: " + person1.toString());
        System.out.println("person1 class: " + person1.getClass().getName());

        // Arrays类实践
        int[] numbers = {3, 1, 4, 1, 5, 9, 2, 6};
        System.out.println("Original array: " + Arrays.toString(numbers));

        // 排序
        Arrays.sort(numbers);
        System.out.println("Sorted array: " + Arrays.toString(numbers));

        // 二分查找
        int index = Arrays.binarySearch(numbers, 5);
        System.out.println("Index of 5: " + index);

        // 填充
        int[] filledArray = new int[5];
        Arrays.fill(filledArray, 7);
        System.out.println("Filled array: " + Arrays.toString(filledArray));

        // 比较
        int[] array1 = {1, 2, 3};
        int[] array2 = {1, 2, 3};
        int[] array3 = {1, 2, 4};
        System.out.println("array1 equals array2: " + Arrays.equals(array1, array2));
        System.out.println("array1 equals array3: " + Arrays.equals(array1, array3));


        // Math类实践
        System.out.println("Math.abs(-5): " + Math.abs(-5));
        System.out.println("Math.sqrt(25): " + Math.sqrt(25));
        System.out.println("Math.pow(2, 3): " + Math.pow(2, 3));
        System.out.println("Math.max(10, 20): " + Math.max(10, 20));
        System.out.println("Math.min(10, 20): " + Math.min(10, 20));
        System.out.println("Math.random(): " + Math.random()); // 0.0到1.0之间的随机数

        // 生成特定范围的随机数
        int min = 10, max = 20;
        int randomNum = min + (int)(Math.random() * ((max - min) + 1));
        System.out.println("Random between " + min + " and " + max + ": " + randomNum);

        // Date和Calendar实践
        // 旧版Date（已不推荐用于日期操作）
        java.util.Date oldDate = new java.util.Date();
        System.out.println("Old Date: " + oldDate);

        // Calendar类
//        java.util.Calendar calendar = java.util.Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        System.out.println("Current time: " + calendar.getTime());
//        System.out.println("Year: " + calendar.get(java.util.Calendar.YEAR));
        System.out.println("Year: " + calendar.get(calendar.YEAR));
        System.out.println("Month: " + (calendar.get(calendar.MONTH) + 1)); // 月份从0开始
//        System.out.println("Month: " + (calendar.get(java.util.Calendar.MONTH) + 1)); // 月份从0开始
        System.out.println("Day: " + calendar.get(calendar.DAY_OF_MONTH));

        // 添加时间
        calendar.add(java.util.Calendar.DAY_OF_MONTH, 7);
        System.out.println("Date after 7 days: " + calendar.getTime());

        // 新版Java时间API (Java 8+)
//        java.time.LocalDate localDate = java.time.LocalDate.now();
        LocalDate localDate = LocalDate.now();
        System.out.println("LocalDate: " + localDate);

        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("LocalDateTime: " + localDateTime);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println("Formatted: " + localDateTime.format(formatter));
    }
}


// 用于演示equals和hashCode的类
class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
